<template>
  <div class="task-add">
    <div class="shade"></div>
    <div class="popup">
      <div class="title justify-content-between">
        <span class="fs28 colorFFF">{{ title }}</span>
        <img src="../assets/images/icon-close.png" @click="cancel()" />
      </div>
      <div class="lists">
        <div class="list">
          <div class="key">任务名称：</div>
          <input
            class="val"
            type="text"
            v-model="taskInfo.taskName"
            placeholder="请输入任务名称"
          />
        </div>
        <div class="list">
          <div class="key">任务数量：</div>
          <input
            class="val"
            type="number"
            v-model="taskInfo.taskNum"
            placeholder="请输入任务数量"
          />
          <div class="unit">个</div>
        </div>
      </div>
      <div class="btns justify-content-center">
        <div class="btn sure" @click="save" v-loading="loading">确认</div>
        <div class="btn cancel" @click="cancel()">取消</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "TaskAdd",
  props: {
    data: Object,
  },
  data() {
    return {
      title: "新增任务",
      loading: false,
      taskInfo: {
        taskName: "",
        taskNum: "",
      },
    };
  },
  created() {
    if (this.data.id) {
      this.title = "修改任务";
      Object.assign(this.taskInfo, this.data);
    } else {
      this.title = "新增任务";
    }
  },
  methods: {
    /**
     * 关闭弹窗
     * @param {Number} refresh - 1刷新列表
     */
    cancel(refresh) {
      this.$emit("cancel", refresh);
    },
    // 保存数据
    save() {
      let _this = this;
      if (_this.taskInfo.taskName == "") {
        _this.$message({
          message: "任务名称不能为空",
          type: "warning",
        });
      } else if (_this.taskInfo.taskNum === "") {
        _this.$message({
          message: "任务数量不能为空",
          type: "warning",
        });
      } else if (_this.taskInfo.taskNum <= 0) {
        _this.$message({
          message: "任务数量必须大于0",
          type: "warning",
        });
      } else {
        let param = {
          list: [_this.taskInfo]
        }
        _this.loading = true;
        _this.axios
          .taskSave(param)
          .then((res) => {
            console.log(res);
            if (res.code == 200) {
              _this.$message({
                message: res.msg,
                type: "success",
              });
              _this.cancel(1);
            } else {
              _this.$message.error(res.msg);
            }
            _this.loading = false;
          })
          .catch(function (error) {
            _this.loading = false;
            _this.$message.error(error.msg);
          });
      }
    },
  },
};
</script>

<style scoped lang='less'>
.task-add {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
  .shade {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.3);
  }
  .popup {
    width: 6.39rem;
    height: 4.22rem;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    box-shadow: 0 .05rem .15rem 0 rgba(0, 21, 54, 0.25);
    .title {
      width: 100%;
      height: .58rem;
      background-color: #285fe3;
      padding: 0 .23rem 0 .28rem;
      img {
        width: .23rem;
        height: .23rem;
        cursor: pointer;
      }
    }
    .lists {
      margin-top: .57rem;
    }
    .list {
      width: 100%;
      margin-top: .24rem;
      padding-left: .79rem;
      display: flex;
      align-items: center;
      .key {
        width: 1.37rem;
        font-size: .24rem;
        color: #161d2e;
      }
      .val {
        width: 3.2rem;
        height: .44rem;
        border: 1px solid #aab3c4;
        font-size: .2rem;
        color: #505971;
        padding-left: .12rem;
        font-family: "Microsoft YaHei,Arial";
      }
      .val::placeholder {
        color: #aab3c4;
      }
      .val:focus {
        outline: 1px solid #316eff;
        color: #161d2e;
      }
      .unit {
        font-size: .18rem;
        color: #161d2e;
        margin-left: .06rem;
      }
    }
    .list:first-child {
      margin-top: 0;
    }
    .btns {
      width: 100%;
      margin-top: .67rem;
      .btn {
        width: 1.3rem;
        height: .4rem;
        margin-left: .69rem;
        line-height: .4rem;
        text-align: center;
        border-radius: .03rem;
        font-size: .22rem;
        cursor: pointer;
      }
      .btn.sure {
        background: linear-gradient(0deg, #00a366 0%, #00c37a 100%);
        color: #fff;
      }
      .btn.cancel {
        border: 1px solid #316eff;
        background: linear-gradient(0deg, #d9dde8 0%, #ffffff 100%);
        color: #505971;
      }
      .btn:first-child {
        margin-left: 0;
      }
    }
  }
}
</style>